package com.heer.gymnasium.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.model.SiteOfferBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class RechargeOfferAdapter extends ListAdapter<SiteOfferBean> {
    private int temp = -1;
    private boolean isCancel;

    public RechargeOfferAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = mInflater.inflate(R.layout.item_recharge_offer, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        SiteOfferBean item = getItem(position);
        holder.radioPackage.setText("充值" + item.getMoney() + "元");
        holder.tvOffer.setText("送" + item.getGift() + "元");
        holder.tvDiscount.setText((int) (Double.valueOf(item.getMoney())
                / (Double.valueOf(item.getMoney()) + Double.valueOf(item.getGift()))
                * 100) + "折");

        if (temp == position) {
            holder.radioPackage.setChecked(!isCancel);
        } else {
            holder.radioPackage.setChecked(false);
        }

        return view;
    }

    public void setTemp(int temp) {
        if (this.temp == temp) {
            isCancel = !isCancel;
        } else {
            isCancel = false;
        }

        if (isCancel)
            this.temp = -1;
        else
            this.temp = temp;

        notifyDataSetChanged();
    }

    public int getTemp() {
        return temp;
    }

    static class ViewHolder {
        @BindView(R.id.radio_package)
        RadioButton radioPackage;
        @BindView(R.id.tv_offer)
        TextView tvOffer;
        @BindView(R.id.tv_discount)
        TextView tvDiscount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
